package com.gg.registration.user.api;


import com.gg.common.result.CommonResult;
import com.gg.common.result.ResultCodeEnum;
import com.gg.common.utils.AuthContextUtils;
import com.gg.registration.model.user.UserInfo;
import com.gg.registration.user.service.UserInfoService;
import com.gg.registration.vo.user.LoginVo;
import com.gg.registration.vo.user.UserAuthVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;


/**
 * Author：  chenjunjia
 * Date：    2022/4/19 0:12
 * WeChat：  China_JoJo_
 */
@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {

    @Resource
    private UserInfoService userInfoService;

    /**
     * 用户手机号 + 阿里云短信服务登陆
     * 【注意】：发送短信微服务和此微服务是分离的，不做耦合调用
     *         登陆模块只做验证
     * @param loginVo 信息封装对象
     * @return 统一返回结果集
     */
    @PostMapping("login")
    public CommonResult login(@RequestBody LoginVo loginVo) {
        HashMap<String,Object> resultMap = userInfoService.login(loginVo);
        return null==resultMap? CommonResult.fail() : new CommonResult(ResultCodeEnum.SUCCESS,resultMap);
    }

    /**
     * 用户认证功能，根据从token拿到的用户id来修改(添加上其他的)用户信息
     * @param userAuthVo 实名认证信息封装对象
     * @param request request
     * @return 统一返回结果
     */
    @PostMapping("auth/userAuth")
    public CommonResult authUser(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        boolean result = userInfoService.authUser(AuthContextUtils.getUserId(request),userAuthVo);
        return result
                ? new CommonResult(ResultCodeEnum.SUCCESS,"成功发送实名认证信息，请等待工作人员审核……")
                : new CommonResult(ResultCodeEnum.FAIL,"发送实名认证信息失败");
    }

    /**
     * 根据用户id获取用户信息
     * @param request request
     * @return 统一返回结果
     */
    @GetMapping("auth/getUserInfoById")
    public CommonResult getUserInfoById(HttpServletRequest request) {
        Long userId = AuthContextUtils.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return null != userInfo
                ? new CommonResult(ResultCodeEnum.SUCCESS,userInfo)
                : new CommonResult(ResultCodeEnum.FAIL,"此用户未能获取到");
    }
}
